package com.neu.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neu.domain.Registered;
import com.neu.domain.User;
import com.neu.service.RegisteredService;
import com.neu.utils.JsonModel;

@Controller
@RequestMapping("/registered")
public class RegisteredController {
	@Resource(name="registeredServic")
	private RegisteredService registeredService;
	
	@RequestMapping("/addregistered")
	public String addregistered(Registered registered,Model model) {
		/*System.out.println(registered);*/
		//调用service方法插入数据
		String message=registeredService.insertRegistered(registered);
		//将挂号成功与否的信息返回给页面
		model.addAttribute("message", message);
		//不关挂号成功与否都跳转到挂号页面
		return "forward:/home/toRegistered";
	}
	
	
	@RequestMapping("/findAllPerson")
	@ResponseBody
	public List<Registered> findAllRegistered() {
		//===============假数据===============================
		/*List<Registered> list=new ArrayList<>();
		Registered r1=new Registered(1, "哈哈", 10, new Date(),"131","daa", new Reglevel(1,"专家",20.0), new Dept(1,"精神科"), new Doctor(3,"lsis",null), 0, 20.0, new Date(), 0, 0);
		list.add(r1);list.add(r1);list.add(r1);list.add(r1);list.add(r1);list.add(r1);list.add(r1);list.add(r1);list.add(r1);list.add(r1);list.add(r1);list.add(r1);list.add(r1);list.add(r1);
		for(Registered r:list) {
			System.out.println(r);
		}*/
		//===================调用service获取真实数据============
		
		List<Registered> list=registeredService.findRegisteredRecoed();
		return list;
		/*
		 * 
		 * java中的json集合--{ {} ,{},{}}
		 * js中json数组--[{},{},{}]
		 * 
		 */
  }
	//根据当前登录的医生查询其需要诊断的患者信息
	@RequestMapping("/findPersonBydoc")
	@ResponseBody
	public List<Registered> findRegisteredByDocname(HttpSession session) {
		//获取登录医生的信息
		User user =(User) session.getAttribute("u_doc");
		List<Registered> list=null;
		if(user.getUsername()!=null) {
			list=registeredService.findRegisteredByDocname(user.getUsername());
		}
		return list;	
		
  }
	//根据id查询挂号信息,返回看诊页面
	@RequestMapping("/findRegisteredById")
	public String  findRegisteredById(Integer personid,Model model) {
		//调用service方法获取单个挂号信息
		Registered reg=registeredService.findRegisteredById(personid);
		model.addAttribute("reg", reg);
		return "forward:/home/toCase";
	}
	//根据id查询挂号信息,返回结果数据给客户端
	@RequestMapping("/findRegisteredById_Json")
	@ResponseBody
	public JsonModel  findRegisteredById_Json(Integer personid) {
		JsonModel jsonModel=new JsonModel();		
		//调用service方法获取单个挂号信息
	    Registered reg=registeredService.findRegisteredById(personid);
	    System.out.println("reg:==="+reg);
	    jsonModel.setData(reg);	
	     return jsonModel;
		
	}
	//退号
	@RequestMapping("/backon")
	@ResponseBody
	public JsonModel backon(Integer personid) {
		JsonModel jsonModel=new JsonModel();
		boolean temp=registeredService.updataRegSateById(personid);
		jsonModel.setSuccess(temp);
		return jsonModel;	
	}
	
}
